package Xml;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
/**
 * Created with IntelliJ IDEA.
 * User: Igor
 * Date: 24.11.13
 * Time: 19:34
 * To change this template use File | Settings | File Templates.
 */
public class DOMUserSessionParser {
    String userLogin;
    int[] levelArray;
    int pausedLevel;
    int savedPoints;
    String[] savedAnswersArray;
    Document document;
    String filePath = "D:\\Java\\LegasyTest\\src\\Xml\\UserSessions.xml";
    XPath xpath;
    String query;
    XPathExpression expr;
    Object result;
    NodeList nodeList;

    /**
     * Метод заполняет поля класса которые соответствуют полям userSession по логину юзера.
     * @param userName
     * @throws XPathExpressionException
     * @throws IOException
     * @throws SAXException
     * @throws ParserConfigurationException
     */
    private void createUserSessionParameters(String userName) throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
        userLogin = userName;
        loadDocument();
        XPathFactory factory = XPathFactory.newInstance();
        xpath = factory.newXPath();
        readLevelArrayFromFile();
        readPausedLevelFromFile();
        readSavedPointsFromFile();
        readSavedAnswersArrayFromFile();
    }

    private void loadDocument() throws ParserConfigurationException, IOException, SAXException {
        File xmlFile = new File(filePath);
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        document = documentBuilder.parse(xmlFile);
        document.getDocumentElement().normalize();
    }

    private void readLevelArrayFromFile() throws XPathExpressionException {
        query = "//userSession[login=" + "'" + userLogin + "'" + "]/levelArray/text()";
        expr = xpath.compile(query);
        result = expr.evaluate(document, XPathConstants.NODESET);
        nodeList = (NodeList)result;
        String stringLevelArray = nodeList.item(0).getNodeValue();
        levelArray = getIntArray(stringLevelArray);
    }

    private void readPausedLevelFromFile() throws XPathExpressionException {
        query = "//userSession[login=" + "'" + userLogin + "'" + "]/pausedLevel/text()";
        expr = xpath.compile(query);
        result = expr.evaluate(document, XPathConstants.NODESET);
        nodeList = (NodeList)result;
        pausedLevel = Integer.parseInt(nodeList.item(0).getNodeValue());
    }

    private void readSavedPointsFromFile() throws XPathExpressionException {
        query = "//userSession[login=" + "'" + userLogin + "'" + "]/savedPoints/text()";
        expr = xpath.compile(query);
        result = expr.evaluate(document, XPathConstants.NODESET);
        nodeList = (NodeList)result;
        savedPoints = Integer.parseInt(nodeList.item(0).getNodeValue());
    }

    private void readSavedAnswersArrayFromFile() throws XPathExpressionException {
        query = "//userSession[login=" + "'" + userLogin + "'" + "]/savedAnswers/text()";
        expr = xpath.compile(query);
        result = expr.evaluate(document, XPathConstants.NODESET);
        nodeList = (NodeList)result;
        savedAnswersArray = nodeList.item(0).getNodeValue().split(",");
    }

    private int[] getIntArray(String string) {
        String[] stringArray = string.split(",");
        int[] intArray = new int[stringArray.length];
        for (int i = 0; i < stringArray.length;  i++) {
            intArray[i] = Integer.parseInt(stringArray[i]);
        }
        return intArray;
    }

    public int[] getLevelArray() {
        return levelArray;
    }

    public int getPausedLevel() {
        return pausedLevel;
    }

    public int getSavedPoints() {
        return savedPoints;
    }

    public String[] getSavedAnswersArray() {
        return savedAnswersArray;
    }
}
